package crackerx.game.level;

import java.util.ArrayList;
import java.util.List;

import crackerx.game.graphics.GameBitMaps;
import crafex.engine.entities.Entity;
import crafex.engine.entities.Player;
import crafex.engine.graphics.GraphicTools;
import crafex.engine.level.Level;
import crafex.engine.level.LevelHandler;
import crafex.engine.system.Command;
import crafex.engine.system.Console;
import crafex.engine.system.Crafex;
import crafex.engine.tools.Button;
import crafex.engine.tools.FloatVertex2D;
import crafex.engine.tools.IntVertex2D;
import crafex.engine.tools.Tools;
import crafex.resource.objects.Bullet;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;


public class Game extends Level{
	
	/**serial thing*/
	private static final long serialVersionUID = -482974887567444593L;
	private boolean buttonPressed = false;
	@SuppressWarnings("unused")
	private boolean buttonPressed2 = false;
	//TODO make button pressed 2 do something
	private boolean moving1 = false;
	private int difficulty;
	private long loadedTime;
	private boolean loaded;
	
	private static Player player;

	public Game(int difficulty) {
		super(new IntVertex2D(3000, 3000));
		loadedTime = System.currentTimeMillis() + 1000;
		resetStatics();
		this.difficulty = difficulty;
		start();
	}
	
	public void start(){
		createGUI();
		background = GameBitMaps.background;
		
		player = new Player(GraphicTools.add(GameBitMaps.player_arms_pistol, GameBitMaps.player), 3, 4);
	}
	
	public void loadStart(){
		createGUI();
		background = GameBitMaps.background;
	}
	
	public void createGUI(){
		char pause = '~' + 1;
		clearGUI();
		addToGUI(new Button(GraphicTools.makeTextButtonImage("fire", GameBitMaps.four_letter_button,
				GameBitMaps.purple_text, new IntVertex2D(6, 6), false), 85, 
				Crafex.getScreenHeight() - 30, new Command(this, "playerShoot")));
		addToGUI(new Button(GraphicTools.makeTextButtonImage(String.valueOf(pause), GameBitMaps.one_letter_button, GameBitMaps.purple_text, new IntVertex2D(6, 6), false),
				Crafex.getScreenWidth() - 35, 35, new Command(Crafex.levelHandler, "pause")));
		
		List<Button> pauseGUI = new ArrayList<Button>();
		pauseGUI.add(new Button(GraphicTools.makeTextButtonImage("resume", GameBitMaps.six_letter_button, GameBitMaps.purple_text, new IntVertex2D(6, 6), false), 
				Crafex.getScreenWidth() / 2, Crafex.getScreenHeight() / 3, new Command(Crafex.levelHandler, "unpause")));
		//TODO add rest of pause menu
		
		LevelHandler.setPauseGUI(pauseGUI);
	}
	
	public void playerShoot(){
		if(player.getLastMovement().getX() != 0 && player.getLastMovement().getY() != 0){
			projectiles.add(new Bullet(GameBitMaps.bullet,
				new FloatVertex2D(player.getLastMovement().getX() / player.getSpeed(),
						player.getLastMovement().getY() / player.getSpeed()),
				player.getCenter(),
				100, 10));//TODO make shooting
		}else{
			projectiles.add(new Bullet(GameBitMaps.bullet,
					new FloatVertex2D(0, 1),
					player.getCenter(),
					100, 10));
		}
	}
	
	public static void playerChangeWeapon(int slot){
		
	}
	
	public int getDifficulty() {
		return difficulty;
	}

	public static FloatVertex2D getPlayerVertex(){
		return player.getCoordinates();
	}
	
	public static IntVertex2D findOpenSpace(IntVertex2D size){
		//TODO update finding open space
		IntVertex2D data = new IntVertex2D(
				Tools.getRandomInt(0, bounds.getX()) - 25,
				Tools.getRandomInt(0, bounds.getY() - 25));
		
		return data;
	}
	
	private void end(){
		//TODO make games ending thing
		
	}
	
	public void update(){
		if(loaded){
			if(LevelHandler.isPlaying()){
				
				if(moving1){
					player.controls(new FloatVertex2D(point.getX() + scroll.getX(), point.getY() + scroll.getY()));
				}
				
				player.update();
				
				updateList(projectiles);
				
			}
			
			
		}else if(loadedTime <= System.currentTimeMillis()){
			loaded = true;
		}
		if(player.getHealth() <= 0){
			end();
		}
	}
	
	private void updateList(ArrayList<Entity> list){
		for(int i = 0; i < list.size(); i++){
			if(list.get(i).getCoordinates().getX() + list.get(i).getSprite().getWidth() <= 0 ||
					list.get(i).getCoordinates().getY() + list.get(i).getSprite().getHeight() <= 0 ||
					list.get(i).getCoordinates().getX() >= Level.bounds.getX() ||
					list.get(i).getCoordinates().getY() >= Level.bounds.getY()){
				list.remove(i);
				break;
			}
			list.get(i).update();
		}
		for(int i = 100; i < list.size(); i++){
			list.remove(1);
		}
	}
	
	public void draw(Canvas canvas){
		if(loaded){
			if(background != null){
				for(int w = 0; w - background.getWidth() < bounds.getX(); w += background.getWidth()){
		    		for(int h = 0; h - background.getHeight() < bounds.getY(); h += background.getHeight()){
		    			canvas.drawBitmap(background, w - scroll.getX(), h - scroll.getY(), null);
		    		}
		    	}
			}else{
				canvas.drawRect(0, 0, Crafex.getScreenWidth(), Crafex.getScreenHeight(), new Paint());
			}
			
			for(Entity e: projectiles){
				e.draw(canvas);
			}
			//TODO fix bullets not showing up
			
			player.draw(canvas);
			
			if(LevelHandler.isPlaying()){
				for(int i = 0; i < GUI.size(); i++){
					GUI.get(i).draw(canvas);
				}
			}else{
				for(int i = 0; i < LevelHandler.getPauseGUI().size(); i++){
					LevelHandler.getPauseGUI().get(i).draw(canvas);
				}
			}
		}else if(loadedTime <= System.currentTimeMillis()){
			loaded = true;
		}else{
			//TODO loading screen
		}
	}
	
	FloatVertex2D point = new FloatVertex2D();
	
	@Override
	public void inputHandler(MotionEvent event){
		if(LevelHandler.isPlaying() && loaded){
			switch (event.getAction() & MotionEvent.ACTION_MASK) {
			
			case MotionEvent.ACTION_DOWN:
				point.setVertex(event.getX(), event.getY());
				for(int i = 0; i < getGUI().size(); i++){
					if(getGUI().get(i).isCollition(event.getX(), event.getY())){
						Console.runCommand(getGUI().get(i).getCommand());
						buttonPressed = true;
					}
				}if(!buttonPressed){
					moving1 = true;
				}
				break;
				
			case MotionEvent.ACTION_MOVE:
				if(moving1){
					point.setVertex(event.getX(), event.getY());
				}
				break;
				
			case MotionEvent.ACTION_UP:
				buttonPressed = false;
				moving1 = false;
				break;
				
			case MotionEvent.ACTION_POINTER_1_DOWN:
				for(int i = 0; i < getGUI().size(); i++){
					if(getGUI().get(i).isCollition(event.getX(), event.getY())){
						Console.runCommand(getGUI().get(i).getCommand());
						buttonPressed2 = true;
					}
				}
				break;
				
			case MotionEvent.ACTION_POINTER_1_UP:
				buttonPressed2 = false;
				break;

			default:
				break;
			}
		}else if(!LevelHandler.isPlaying()){
			for(int i = 0; i < LevelHandler.getPauseGUI().size(); i++){
				if(LevelHandler.getPauseGUI().get(i).isCollition(event.getX(), event.getY())){
					Console.runCommand(LevelHandler.getPauseGUICommand(i));
				}
			}
		}else if(!loaded){
			for(int i = 0; i < LevelHandler.getPauseGUI().size(); i++){
				if(LevelHandler.getPauseGUI().get(i).isCollition(event.getX(), event.getY())){
					Console.runCommand(LevelHandler.getPauseGUICommand(i));
				}
			}
		}
		
	}
}